package com.global.college.views;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import androidx.annotation.DrawableRes;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.global.college.R;


public class TitleBar extends RelativeLayout {

    private LinearLayout lin_back;
    private RelativeLayout rel_background;
    private TextView titleTv;
    private TextView rightTv;
    private TitleBarCallback callback;
    private String title;
    private ImageView rightim;
    private ImageView img_search;
    private ImageView iv_back;
    private View deviceView;

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(final Context context, AttributeSet attrs) {
        View view = LayoutInflater.from(context).inflate(R.layout.title_bar, this, true);
        lin_back = view.findViewById(R.id.lin_back);
        rel_background = view.findViewById(R.id.rel_background);
        titleTv = view.findViewById(R.id.tv_title);
        rightTv = view.findViewById(R.id.tv_right_text);
        rightim = view.findViewById(R.id.img_share);
        img_search = view.findViewById(R.id.img_search);
        iv_back = view.findViewById(R.id.iv_back);
        deviceView = view.findViewById(R.id.view);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CommonTitleBar);
        title = typedArray.getString(R.styleable.CommonTitleBar_title);
        String rightText = typedArray.getString(R.styleable.CommonTitleBar_rightText);
        boolean rightVisibility = typedArray.getBoolean(R.styleable.CommonTitleBar_rightViewVisibility, false);
        typedArray.recycle();

        titleTv.setText(title);
        rightTv.setText(rightText);
        rightTv.setVisibility(rightVisibility ? VISIBLE : GONE);

        lin_back.setOnClickListener(v -> {
            if (callback != null) {
                callback.onBackPress();
            }
        });

        rightTv.setOnClickListener(v -> {
            if (callback != null) {
                callback.onRightPress();
            }
        });

        rightim.setOnClickListener(v -> {
            if (callback != null) {
                callback.onRightImageClick();
            }
        });
        img_search.setOnClickListener(v -> {
            if (callback != null) {
                callback.onSearchImageClick();
            }
        });
    }

    public void setTitleBarCallback(TitleBarCallback callback) {
        this.callback = callback;
    }

    public interface TitleBarCallback {
        void onBackPress();

        void onRightPress();

        void onRightImageClick();

        void onSearchImageClick();
    }

    public TextView getTitleTv() {
        return titleTv;
    }

    public TextView getRightTv() {
        return rightTv;
    }

    public ImageView getReturnIm() {
        iv_back.setVisibility(VISIBLE);
        return iv_back;
    }
    public ImageView getRightIm() {
        rightim.setVisibility(VISIBLE);
        return rightim;
    }
    public ImageView getSearchIm() {
        img_search.setVisibility(VISIBLE);
        return img_search;
    }

    public ImageView getLeftImage() {
        return iv_back;
    }

    public RelativeLayout getBackGround() {
        return rel_background;
    }


    /**
     * 设置返回按键图片
     *
     * @param drawableId
     */
    public void setReturnImageResource(@DrawableRes int drawableId) {
        try {
            iv_back.setVisibility(VISIBLE);
            iv_back.setImageResource(drawableId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 设置右边图片
     *
     * @param drawableId
     */
    public void setRightImageResource(@DrawableRes int drawableId) {
        try {
            rightim.setVisibility(VISIBLE);
            rightim.setImageResource(drawableId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 设置右边搜素图片
     *
     * @param drawableId
     */
    public void setSearchImageResource(@DrawableRes int drawableId) {
        try {
            img_search.setVisibility(VISIBLE);
            img_search.setImageResource(drawableId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置右边文字
     *
     * @param text
     */
    public void setRightText(String text) {
        try {
            if (!TextUtils.isEmpty(text)) {
                rightTv.setText(text);
                rightTv.setVisibility(VISIBLE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置右边文字
     *
     * @param text
     */
    public void setRightText(int text) {
        rightTv.setText(text);
        rightTv.setVisibility(VISIBLE);

    }

    public void setTitleTextColor(int color) {
        ColorStateList csl = getResources().getColorStateList(color);
        titleTv.setTextColor(csl);
    }

    /**
     * 设置标题
     *
     * @param text
     */
    public void setTitleText(String text) {
        try {
            if (!TextUtils.isEmpty(text)) {
                titleTv.setText(text);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setTitleText(int text) {
        titleTv.setVisibility(VISIBLE);
        titleTv.setText(text);
    }

    public void setDeviceViewVisibility(boolean isVisibility) {
        if (isVisibility) {
            deviceView.setVisibility(VISIBLE);
        } else {
            deviceView.setVisibility(GONE);
        }
    }

}

